package edu.usm.roberge.server.orbitz;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

/**
 * This class carries around web page data related to the orbitz website
 */
public class OrbitzData {
	
	private Map<String, String> cookies = new HashMap<String, String>();
	private Map<String, String> hiddenParams = new HashMap<String, String>();
	private String submissionUrl;
	private String resultsUrl;

	public void setCookie(String string) {
		int index = string.indexOf('=');
		cookies.put(string.substring(0, index), string.substring(index + 1).trim());
	}
	
	public String getCookies() {
		StringBuffer buf = new StringBuffer();
		for(Entry<String, String> cookie : cookies.entrySet()) {
			buf.append(cookie.getKey());
			buf.append("=");
			buf.append(cookie.getValue());
			buf.append(" ");
		}
		
		return buf.toString();
	}

	public void setSubmissionUrl(String extractSubmissionUrl) {
		submissionUrl = extractSubmissionUrl;
	}

	public void addHiddenParameter(String group, String group2) {
		hiddenParams.put(group, group2);
	}

	public Set<Entry<String, String>> getHiddenParams() {
		return hiddenParams.entrySet();
	}

	public String getSubmissionUrl() {
		return submissionUrl;
	}

	public void setResultsUrl(String group) {
		this.resultsUrl = group;
	}
	
	public String getResultsUrl() {
		return resultsUrl;
	}

	public void addCookies(List<String> cookies2) {
		for(String cookie : cookies2) {
			setCookie(parseCookie(cookie));
		}
	}
	
	private String parseCookie(String cookie) {
		int index = cookie.indexOf(';');
		return cookie.substring(0, index + 1);
	}
	
	@Override
	public String toString() {
		return "[submissionUrl=" + submissionUrl + ", resultsUrl=" + resultsUrl + "]";
	}
}
